<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="70%"
    :before-close="handleClose"
    class="dialogClass"
  >
    <el-form ref="ruleForm" :model="ruleForm" label-width="80px">
      <el-row :gutter="20">
        <el-col :xl="6" :lg="8">
          <el-form-item label="管理号" prop="guanlihao">
            <el-input
              size="big"
              v-model="ruleForm.guanlihao"
              placeholder="请输入管理号"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="牛舍" prop="niushe">
            <el-input
              size="big"
              v-model="ruleForm.niushe"
              placeholder="请输入牛舍"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="产犊日期" prop="chandu">
            <el-input
              size="big"
              v-model="ruleForm.chandu"
              placeholder="请输入产犊日期"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="胎次" prop="taici">
            <el-input
              size="big"
              v-model="ruleForm.taici"
              placeholder="请输入胎次"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="早/正产" prop="zaozhengchan">
            <el-input
              size="big"
              v-model="ruleForm.zaozhengchan"
              placeholder="请输入早/正产"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="死/活胎" prop="sihuotai">
            <el-input
              size="big"
              v-model="ruleForm.sihuotai"
              placeholder="请输入死/活胎"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="胎衣情况" prop="taiyi">
            <el-input
              size="big"
              v-model="ruleForm.taiyi"
              placeholder="请输入胎衣情况"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="产后天数" prop="chanhou">
            <el-input
              size="big"
              v-model="ruleForm.chanhou"
              placeholder="请输入产后天数"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="检查时间" prop="jiancha">
            <el-input
              size="big"
              v-model="ruleForm.jiancha"
              placeholder="请输入检查时间"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="子宫检查" prop="zigong">
            <el-input
              size="big"
              v-model="ruleForm.zigong"
              placeholder="请输入子宫检查"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="卵巢检查" prop="luanchao">
            <el-input
              size="big"
              v-model="ruleForm.luanchao"
              placeholder="请输入卵巢检查"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="检查人" prop="jiancharen">
            <el-input
              size="big"
              v-model="ruleForm.jiancharen"
              placeholder="请输入检查人"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="8">
          <el-form-item label="操作人" prop="caozuoren">
            <el-input
              size="big"
              v-model="ruleForm.caozuoren"
              placeholder="请输入操作人"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        size="big"
        @click="onSubmit('ruleForm')"
        :loading="loading"
        >{{ submitButton }}</el-button
      >
      <el-button size="big" @click="resetForm('ruleForm')">重置</el-button>
      <el-button size="big" @click="close('ruleForm')">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveexamin, updateexamin } from "@/apis/store/examinationrecords.js";


export default {
  name: "addCategory",
  data() {
    return {
      // 提交按钮
      submitButton: "",
      // 控制弹窗
      open: false,
      // 控制按钮加载状态
      loading: false,
      // 弹窗标题
      title: "",
      // 当前库房
      // 表单项
      ruleForm: {
        sihuotai: "",
        niushe: "",
        caozuoren: "",
        taici: "",
        luanchao: "",
        chandu: "",
        chanhou: "",
        zigong: "",
        guanlihao: "",
        jiancha: "",
        jiancharen: "",
        taiyi: "",
        zaozhengchan: "",
      },
    };
  },
  methods: {
    // 控制弹窗
    show(title, row) {
      const _this = this;
      _this.title = title;
      _this.open = true;
      if (row) {
        _this.submitButton = "修改";
        _this.ruleForm = JSON.parse(JSON.stringify(row));
      } else {
        _this.submitButton = "确定";
      }
    },
    //关闭
    close(formName) {
      this.open = false;
      this.resetForm(formName);
    },
    //点击关闭
    handleClose(done) {
      const _this = this;
      _this
        .$confirm("系统可能不会保存您的更改", "确认关闭？")
        .then((_) => {
          done();
          _this.resetForm("ruleForm");
        })
        .catch((_) => {});
    },
    //点击提交
    onSubmit(formName) {
      const _this = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (_this.title == "新增检查记录") {
            saveexamin(_this.ruleForm).then(function (res) {
              console.log("res: ", res.code);
              if (res.code === 0) {
                _this.$alert(_this.ruleForm.guanlihao + res.msg, "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    _this.close("ruleForm");
                    _this.$parent.$parent.search();
                  },
                });
              }
            });
          } else {
            updateexamin(_this.ruleForm).then(function (res) {
              if (res.code === 0) {
                _this.$alert(_this.ruleForm.guanlihao + res.msg, "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    _this.close("ruleForm");
                    _this.$parent.$parent.search();
                  },
                });
              }
            });
          }
        }
      });
    },
    // 表单重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
</style>